<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="序列号" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['serialNumber', validatorRules.serialNumber]" placeholder="请输入序列号"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="制造商标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['sign', validatorRules.sign]" placeholder="请输入制造商标识"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="机型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <model ref='model' />
              <!--<a-input v-decorator="['model', validatorRules.model]" placeholder="请输入无人机类型"  ></a-input>-->
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="应用类型" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['agrPlaneModel', validatorRules.agrPlaneModel]" :trigger-change="true" dictCode="types_of_farm" placeholder="请选择农机类型" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-item label="作业总面积（亩）" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input-number v-decorator="['totalArea', validatorRules.totalArea]" placeholder="请输入作业总面积（亩）" style="width: 100%" />
            </a-form-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-item label="实名登记标识" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['realSign', validatorRules.realSign]" placeholder="请输入实名登记标识"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="运营人" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['operator', validatorRules.operator]" placeholder="请输入运营人"  ></a-input>
            </a-form-item>
          </a-col>
        <!--  <a-col :span="24">
            <a-form-item label="激活时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择激活时间" v-decorator="['activationTime', validatorRules.activationTime]" :trigger-change="true" style="width: 100%" />
            </a-form-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-item label="激活状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['activationState', validatorRules.activationState]" :trigger-change="true" dictCode="activated_state" placeholder="请选择激活状态" />
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="销售状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="list" v-decorator="['salesStatus', validatorRules.salesStatus]" :trigger-change="true" dictCode="sales_status" placeholder="请选择销售状态" />
            </a-form-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-item label="App用户" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['userName', validatorRules.userName]" placeholder="请输入App用户"  ></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="用户组" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input v-decorator="['userDeparts', validatorRules.userDeparts]" placeholder="请输入用户组"  ></a-input>
            </a-form-item>
          </a-col>-->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import model from "@/components/axhk/model"

  export default {
    name: 'AxhkYwAgrplaneForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      model
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          serialNumber: {
            rules: [
              { required: true, message: '请输入序列号!'},
              { validator: (rule, value, callback) => validateDuplicateValue('axhk_yw_agrplane', 'serial_number', value, this.model.id, callback)},
            ]
          },
          sign: {
            rules: [
              { required: true, message: '请输入制造商标识!'},
            ]
          },
          model: {
            rules: [
              { required: true, message: '请输入机型!'},
            ]
          },
          agrPlaneModel: {
            rules: [
              { required: true, message: '请输入应用类型!'},
            ]
          },
          totalArea: {
            rules: [
              { required: false},
              { pattern: /^-?\d+\.?\d*$/, message: '请输入数字!'},
            ]
          },
          realSign: {
            rules: [
              { required: true, message: '请输入实名登记标识!'},
            ]
          },
          operator: {
            rules: [
              { required: true, message: '请输入运营人!'},
            ]
          },
          activationTime: {
            rules: [
              { required: true, message: '请输入激活时间!'},
            ]
          },
          activationState: {
            rules: [
              { required: true, message: '请输入激活状态!'},
            ]
          },
          salesStatus: {
            rules: [
              { required: true, message: '请输入销售状态!'},
            ]
          },
          userName: {
            rules: [
              { required: true, message: '请输入App用户!'},
            ]
          },
          userDeparts: {
            rules: [
              { required: true, message: '请输入用户组!'},
            ]
          },
        },
        url: {
          add: "/fly/axhkYwAgrplane/add",
          edit: "/fly/axhkYwAgrplane/edit",
          queryById: "/fly/axhkYwAgrplane/queryById",
          update1:"/fly/axhkYwAgrplane/getByTimes"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'serialNumber','sign','model','agrPlaneModel','totalArea','realSign','operator', 'activationTime','activationState','salesStatus','userName','userDeparts'))
          if(record.id){//编辑
            //alert(record.model_dictText)
            //this.$refs.model.filterOption(record.model,record.model_dictText)
          }else{
            this.$refs.model.reset();
          }
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
              
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
           
          
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            // alert(JSON.stringify(values))
            console.log("表单提交数据",formData)        
            httpAction(httpurl,formData,method).then((res)=>{
               if(values.activationState){
                      getAction(this.url.update1,{num:values.activationState,number:values.serialNumber}).then((res)=>{
                        if(res.success){
                          this.edit (res.result);                      
                        }
                      });
                  }
              
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'serialNumber','sign','model','agrPlaneModel','totalArea','realSign','operator','activationTime','activationState','salesStatus','userName','userDeparts'))
      },
    }
  }
</script>